/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.rest;

import cz.mzk.rehan.commons.DatastreamId;
import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
class UpdateDatastreamInlineContentOperation extends AbstractOperation {

    public static final Logger logger = Logger.getLogger(UpdateDatastreamInlineContentOperation.class.getName());

    public UpdateDatastreamInlineContentOperation(FedoraServer fedoraServer) {
        super(new HttpConnector(fedoraServer, HttpMethod.POST));
    }

    public boolean run(String subjectPid, DatastreamId dsId, Document dsContent) {
        String request = buildRequest(subjectPid, dsId);
        logger.log(Level.FINE, "request: ''{0}''", request);
        try {
            String result = connector.processRequestWithStringResult(request, dsContent);
            logger.log(Level.FINE, "result: ''{0}''", result);
            return true;
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
            return false;
        }
    }

    private String buildRequest(String subjectPid, DatastreamId datastreamId) {
        StringBuilder builder = new StringBuilder();
        builder.append(getBaseUrl());
        builder.append("/objects/").append(subjectPid);
        builder.append("/datastreams/").append(datastreamId.toString());
        return builder.toString();
    }
}
